
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>总仓出入库</title>
    <link href="../../rs/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
  	<link href="../../rs/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
  	<link href="../../rs/plugins/bootstrap_tab/css/bootstrap-table.css" rel="stylesheet">
  	<link href="../../rs/css/animate.min.css" rel="stylesheet">
  	<link href="../../rs/css/style.min862f.css?v=4.1.0" rel="stylesheet">
  	<link href="../../rs/plugins/datapicker/datepicker3.css" rel="stylesheet">
  	<link href="../../rs/plugins/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet">
    <?php
        require_once('login.php');
     ?>
    <style>
        #toolbar1 {
            padding: 0px 15px;
            float: right;
        }
        .btn-group-sm > .btn, .btn-sm {
            padding: 5px 12px;
        }

        .editable-click, a.editable-click, a.editable-click:hover {
            border-bottom: 0px;
        }
        .pull-right{ width:300px}
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins" style="    background: #fff;padding: 20px;">
        <div class="row">
            <div class="col-sm-4 m-b-xs">
                <div class="form-group" id="data_1">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input type="text" class="form-control" placeholder="请选择单据日期" name="start" id="start" readonly="readonly">
                    </div>
                </div>
            </div>
            <div class="col-sm-4 m-b-xs"></div>
            <div class="col-sm-4 m-b-xs">
                <div id="toolbar1">
                    <!-- <button onclick="cancelall()" style="float:left;margin-left:5px" class="btn" id="del">
                        <i class="glyphicon glyphicon-remove"></i> 删除
                    </button> -->
                    <button  style="float:left;margin-left:5px" class="btn">
                        <i class="glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button onclick="save_do()" style="float:left;margin-left:5px" class="btn btn-success">
                        <i class="glyphicon glyphicon-ok"></i> 保存
                    </button>
                    <button onclick="save_sh()" style="float:left;margin-left:5px" class="btn btn-danger">
                        <i class="glyphicon glyphicon-zoom-in"></i> 审核
                    </button>
                </div>
            </div>



        </div>

        <div class="row">
            <div class="col-sm-4 m-b-xs">
                <div class="form-group draggable">
                    <div class="input-group">
                        <span class="input-group-btn">
                            <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px;margin-left:-1px" type="button" class="btn btn-sm ">订 单</button>
                        </span>
                        <select class="form-control" id="selectorder">
                            <option value="-1">新单</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="input-group ">
                        <span class="input-group-btn">
                            <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px;margin-left:-1px" type="button" class="btn btn-sm ">仓库</button>
                        </span>
                    <select class="form-control" id="m_ck">
                    </select>
                </div>
            </div>

            <div class="col-sm-4">
                <div class="input-group ">
                          <span class="input-group-btn">
                              <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">客户资料</button>
                          </span>
                    <select class="form-control" id="m_kh">
                    </select>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="input-group">
                    <div class="input-group-btn">
                        <button style="height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">
                            订单备注
                        </button>
                    </div>
                    <input type="text" class="form-control" id="djbz">
                </div>
            </div>
        </div>
    </div>
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="row">
                <div>
                    <div class="col-sm-4">
                        <button style="float:left;  margin-right:5px;margin-bottom: 20px;height:34px;" id="remove" class="btn btn-danger" disabled>
                            <i class="glyphicon glyphicon-trash" style="font-size:12px"></i>
                        </button>
                    </div>
                    <div class="col-sm-4 m-b-xs" style="float: right;">
                        <div class="input-group">
                            <div class="input-group-btn">
                                <button style="height:34px;margin-left:-1px;" type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                    产品资料
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                            </div>
                            <input type="text" class="form-control" id="searchtxt" onkeydown="_key()">
                            <div class="input-group-btn">
                                <button style="height:34px" type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                    搜索
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <table id="table"></table>
            <!-- End Example Card View -->
        </div>
    </div>
</div>
<!-- End Panel Other -->

<script src="../../rs/js/jquery.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/bootstrap-table.min.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="../../rs/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/js/jquery.cookie.js"></script>
<script src="../../rs/js/common_qj.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-table-editable.min.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-editable.js"></script>

<script type="text/javascript">
    var $table = $('#table');
    var $remove = $('#remove');
    var sign = URLencode($.cookie('sign'));
    $(function () {
        //日期选择插件
        $("#data_1 .input-group.date").datepicker({
            todayBtn: "linked",
            keyboardNavigation: !1,
            forceParse: !1,
            calendarWeeks: !0,
            autoclose: !0
        });
        var start = (new Date()).format('yyyy-MM-dd');
        $('#start').val(start);
        $('#table').bootstrapTable({
            columns: [
                {
                    field: 'state',
                    checkbox: true
                },

                {
                    field: 'prid',
                    title: '产品编号',
                    width:'200px',
                    align: 'left'
                },
                {
                    field: 'prname',
                    align: 'left',
                    width:'300px',
                    title: '产品名称'
                },


                {
                    field: 'sj',
                    align: 'center',
                    title: '单价',
                    editable: true,
                    width:'100px',

                },
                {
                    field: 'si',
                    align: 'center',
                    title: '出库/入库',
                      width:'100px',
                    editable: {
                        type: 'select',
                        source: [
                            {value: '1', text: '入库'},
                            {value: '-1', text: '出库'}
                        ]
                    }
                },
                {
                    field: 'sl',
                    align: 'right',
                    title: '数量',
                    width:'100px',
                    editable: true
                },
                {
                    field: 'bz',
                    align: 'center',
                    title: '备注',
                    editable: true
                }
            ],
            advancedSearch: true,
            idTable: "advancedTable",
            shadeClose: true,
            toggle: "table",

            //当修改数值后执行的保存操作
            onEditableSave: function (field, row, oldValue, $el) {
                if (field == 'sl') {
                    if (isNaN(row.sl)) {
                        row.sl = oldValue;
                    }else {
                        row.sl=parseFloat(row.sl).toFixed(2);
                    }
                }
                if (field == 'sj') {
                    if (isNaN(row.sj)) {
                        row.sj = oldValue;
                    }else {
                        row.sj=parseFloat(row.sj).toFixed(2);
                    }
                }
                $('#table').bootstrapTable('load', dtn);
                banhao();
                return true;
            }
        });
        f_sheard();

        <!-- 删除操作 -->
        $table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
        });
        $remove.click(function () {
            layer.confirm('确定要删除这个产品吗？', {
                title: [''],
                btn: ['确定', '取消'] //按钮
            }, function (index) {
                //发送当前删除的数据
                var prids = $.map($table.bootstrapTable('getSelections'), function (row) {
                    console.log(row);
                    return row.prid;
                });

                $table.bootstrapTable('remove', {
               field: 'prid',
               values: prids
           });
                // $.ajax({
                //     type: "post",
                //     url: G_url+'jxc/del_d',
                //     data: 'rids=' + rids + '&sign=' + sign,
                //     dataType: "json",
                //     success: function (aftdata1) {
                //         layer.closeAll();
                //         if (!aftdata1.ok) {
                //             layer.msg(aftdata1.msg);
                //             if (aftdata1.code == '1000') {
                //                 $.cookie('sign', '', {expires: -1});
                //                 $.cookie('username', '', { expires: -1 });
                //                 window.location.href = 'login.html';
                //                 return;
                //             }
                //         }
                //
                //         //删除选中行
                //         var rowindexs = [];
                //         $('input[name="btSelectItem"]:checked').each(function () {
                //             rowindexs.push($(this).data('index'));
                //         });
                //         var ndtn = [];
                //         for (i = 0; i < dtn.length; i++) {
                //             if ($.inArray(i, rowindexs) < 0) ndtn.push(dtn[i]);
                //         }
                //         dtn = ndtn;
                //
                //         $('#table').bootstrapTable('load', dtn);
                //         banhao();
                //         $remove.prop('disabled', true);
                //     },
                //     error: function (XMLHttpRequest, textStatus, errorThrown) {
                //         layer.msg('数据删除失败', {time: 1000});
                //     }
                // });

            }, function (index) {
                layer.close(index);
            });
        });
        <!-- 删除操作 -->

        //隐藏按钮
        var cz1 = $("#selectorder  option:selected").text();
        if(cz1=='新单'){
            $("#del").hide();
        }

    });

    function crk(dt) {
        if(dt=='1'){
            return "入库";
        }else {
            return "出库";
        }
    }

    var dtnheard = {};
    var js = {};
    var sypr = {};
    function f_sheard() {
        layer.load(2);



        /***************/

        //给仓库做循环
        $.ajax({
            type: "post",
            url:G_url+'sysg/see',
            data: 'sign=' + sign,
            dataType: "json",
            success: function (result) {
                layer.closeAll('loading');
                var ff = result.data;
                var html1 = '';
                $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
                    html1 += "<option value='" + b.ShopGroupID + "'>" + b.ShopGroupName + "</option>";//拼接字符串.
                });
                $("#m_ck").append(html1);
                //$("#mdbh1").append(html1);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        //客户资料
        $.ajax({
            type: "post",
            url:G_url+'slcu/see',
            data: 'sign=' + sign,
            dataType: "json",
            success: function (result) {
                layer.closeAll('loading');
                var ff = result.data;
                var html1 = '';
                $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
                    html1 += "<option value='" + b.cuname + "'>" + b.cuname1 + "</option>";//拼接字符串.
                });
                $("#m_kh").append(html1);
                //$("#mdbh1").append(html1);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        /***************/

        //产品资料
        $.ajax({
            type: "post",
            url: G_url + 'slpr/see',
            data: 'sign=' + sign,
            dataType: "json",
            success: function (aftdata) {
                layer.closeAll('loading');


                 dtnheard = aftdata;
                js.value = dtnheard.order;
                sypr = dtnheard.data;




            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    }


    //给button添加回车事件
    function _key() {
        if(event.keyCode ==13){
            open_modal();
            $("#searchtxt").blur();
        }
    }

    function open_modal() {
        var s = $('#searchtxt').val();
        if (window.event.keyCode==13) window.event.keyCode=0;
        //页面层
        layer.open({
            title: ['添加产品'],
            type: 1,
            offset: 'rb',
            shift: 'left',
            area: ['60%', '100%'], //宽高
            shadeClose: true,
            content: '<div style="padding: 10px 20px 0 20px "><table id="table1"></table></div>'
        });

        $('#table1').bootstrapTable('destroy');//删除table表格
        $('#table1').bootstrapTable({//重新建立表格
            columns: [
                {
                    field: 'prid',
                    title: '产品编号'
                },
                {
                    field: 'prname1',
                    title: '产品名称'
                },
                {
                    field: 'prpriceout',
                    title: '单价'
                },
                {
                    field: 'insert',
                    title: '添加',
                    align: 'center',
                    formatter: insertFormatter
                }
            ],
            shadeClose: true,
            searchText: s,
            data: sypr,
            toggle: "table",

            search: true
        });
        banhao();

    }

    function insert_do(r) {
        var lno = 0;
        var sfcz = false;
        for (var i = 0, l = dtn.length; i < l; i++) {
            if (dtn[i].prid == r.prid) {
                lno = i;
                sfcz = true;
                break
            }
        }

        if (sfcz) {
            layer.open({
                content: '数据存已在是否累加?',
                title: [''],
                btn: ['累加', '新增'], //按钮
                yes: function (index) {
                    //累加
                    dtn[lno].sl++;
                    $table.bootstrapTable('load', dtn);
                    banhao();
                    layer.close(index);
                },
                btn2: function (index) {
                    //新增
                    var n = {};
                    n.prid = r.prid;
                    n.prname = r.prname1;
                    n.sj = r.prpriceout;
                    n.sl = 1;
                    n.rid= 0;
                    n.bz=" ";
                    n.si=1;
                    dtn.push(n);

                    $table.bootstrapTable('load', dtn);
                    banhao();
                    layer.close(index);
                },
                cancle: function (index) {
                    layer.close(index);
                }
            });
        } else {
            //新增
            var n = {};
            n.prid = r.prid;
            n.prname = r.prname1;
            n.sj = r.prpriceout;
            n.sl = 1;
            n.rid= 0;
            n.bz=" ";
            n.si=1;
            dtn.push(n);
            $table.bootstrapTable('load', dtn);
            banhao();
        }

    }

    //使用函数将调用的那一行的样式写出
    function insertFormatter(value, row) {
        return "<button type='button' class='btn btn-info' onclick='insert_do(" + JSON.stringify(row) + ")'>添加</button> ";
    }


    var val = -1;
    $("#selectorder").change(function () {
        layer.confirm('确定要选择这个订单吗？', {
            title: [''],
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            var cz = $("#selectorder  option:selected").text();
            var cv = $("#selectorder  option:selected").val();
            val=cv;
            if(cz=='新单'){
                $("#del").hide();
                location.reload();
            }else {
                $("#del").show();
            }
            f_detail(cz);
            layer.close(index);
        }, function () {
            $("#selectorder").val(val);
        });
    });

    var dtn = [];
    var sign1=$.cookie('sign');
    function f_detail(djbh) {
        layer.msg('数据加载中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
        $.ajax({
            type: "post",
            url: G_url+'jxc/sgdd_d',
            data: 'djbh=' + djbh + '&sign=' + sign,
            async:false,
            dataType: "json",
            success: function (aftdata) {
                layer.closeAll();
                if (!aftdata.ok) {
                    layer.msg(aftdata.msg);
                    if (aftdata.code == '1000') {
                        $.cookie('sign', '', {expires: -1});
                        $.cookie('username', '', { expires: -1 });
                        window.location.href = 'login.html';
                        return;
                    }
                }
                dtn = aftdata.detail;

                //数据写进djbz
                $("#selectorder option:selected").each(function(){
                    var l=$(this).index();
                    if(l=="0"){
                        return false;
                    }else {
                        $("#djbz").val(aftdata.order.sjxx);
                        $("#start").val(aftdata.order.djrq);
                    }
                });

                $('#table').bootstrapTable('load', dtn);
                $("#mdbh").val(aftdata.order.shop);
                banhao();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    }

    function cancelall(){
        layer.confirm('确定删除这个订单吗？', {
            title: [''],
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            layer.msg('数据删除中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
            var djbh_del = $("#selectorder  option:selected").text();
            $.ajax({
                type: "post",
                url: G_url+'jxc/del',
                data: 'djbh=' + djbh_del + '&sign=' + sign,
                dataType: "json",
                success: function (aftdata1) {
                    layer.closeAll();
                    if (!aftdata1.ok) {
                        layer.msg(aftdata1.msg);
                        if (aftdata1.code == '1000') {
                            $.cookie('sign', '', {expires: -1});
                            $.cookie('username', '', { expires: -1 });
                            window.location.href = 'login.html';
                            return;
                        }
                    }else {
                        location.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('数据删除失败', {time: 1000});
                }
            });
            layer.close(index);
        }, function (index) {
            layer.close(index);
        });
    }

    var f_data={};
    function save_do(){
        layer.confirm('确定要保存吗？', {
            title: [''],
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            layer.msg('数据保存中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
            var rq=$('#start').val();
            var djbh=$("#selectorder  option:selected").text();
            var mdbh=$("#mdbh  option:selected").val();
            var djbz=$('#djbz').val();
            var data=[{
                "djrq":rq,
                "djbh":djbh,
                "djbz":djbz,
                "shop":mdbh,
                total:"0"
            }];
            var data1=$('#table').bootstrapTable('getData');
            var save={};
            save.order=data;
            save.detail=data1;
            save.sign=sign1;
            $.ajax({
                type: "post",
                url: G_url+'jxc/save',
                data:JSON.stringify(save),
                dataType: "json",
                success: function(res) {
                    if (res.ok){
                        layer.msg(res.msg);
                        f_data=res.detail;

                        var ss = dtnheard.order;
                        if(djbh=="新单"){
                            $("#selectorder").append( "<option value='"+ res.djbh+"' selected>" + res.djbh + "</option>");
                        }
                        $("#del").show();
                        $('#table').bootstrapTable('load', f_data);
                        banhao();
                    } else {
                        layer.msg(res.msg);
                        if (res.code == '1000') {
                            $.cookie('sign', '', {expires: -1});
                            $.cookie('username', '', { expires: -1 });
                            window.location.href = 'login.html';
                            return;
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('数据保存失败', {time: 1000});
                }
            });
            layer.close(index);
        }, function (index) {
            layer.close(index);
        });
    }

    function sh(){
        var rq=$('#start').val();
        var djbh=$("#selectorder  option:selected").text();
        var mdbh=$("#mdbh  option:selected").val();
        var djbz=$('#djbz').val();
        var data=[{
            "djrq":rq,
            "djbh":djbh,
            "djbz":djbz,
            "shop":mdbh,
            total:"0"
        }];
        var data1=$('#table').bootstrapTable('getData');
        var save={};
        save.order=data;
        save.detail=data1;
        save.sign=sign1;
        $.ajax({
            async: false,
            type: "post",
            url: G_url+'jxc/save',
            data:JSON.stringify(save),
            dataType: "json",
            success: function(res) {
                if (res.ok){
                    f_data=res.detail;

                    var ss = dtnheard.order;
                    if(djbh=="新单"){
                        $("#selectorder").append( "<option value='"+ res.djbh+"' selected>" + res.djbh + "</option>");
                    }
                    $("#del").show();
                    $('#table').bootstrapTable('load', f_data);
                    banhao();
                } else {
                    layer.msg(res.msg);
                    if (res.code == '1000') {
                        $.cookie('sign', '', {expires: -1});
                        $.cookie('username', '', { expires: -1 });
                        window.location.href = 'login.html';
                        return;
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('数据保存失败', {time: 1000});
            }
        });
    }

    function save_sh(){
        layer.confirm('确定提交审核吗？', {
            title: [''],
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            sh();
            layer.msg('数据提交中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
            var djbhsh=$("#selectorder  option:selected").text();
            $.ajax({
                async: false,
                type: "post",
                url: G_url+'jxc/sh',
                data:'djbh=' + djbhsh + '&sign=' + sign,
                dataType: "json",
                success: function(res) {
                    if (res.ok){
                        layer.msg(res.msg);
                        location.reload();
                    } else {
                        layer.msg(res.msg);
                        if (res.code == '1000') {
                            $.cookie('sign', '', {expires: -1});
                            $.cookie('username', '', { expires: -1 });
                            window.location.href = 'login.html';
                            return;
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('数据提交失败', {time: 1000});
                }
            });
            layer.close(index);
        }, function (index) {
            layer.close(index);
        });
    }
</script>
</body>
</html>
